package com.scoring.system.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.scoring.system.annotation.SkipSecurityCheck;
import com.scoring.system.pojo.ClassCurriculum;
import com.scoring.system.pojo.Classes;
import com.scoring.system.pojo.Colleage;
import com.scoring.system.pojo.Course;
import com.scoring.system.pojo.Credit;
import com.scoring.system.pojo.ExaminationType;
import com.scoring.system.pojo.Jurisdiction;
import com.scoring.system.pojo.Major;
import com.scoring.system.pojo.Student;
import com.scoring.system.pojo.Term;
import com.scoring.system.pojo.User;
import com.scoring.system.pojo.UserJurisdiction;
import com.scoring.system.service.ClassCurriculumService;
import com.scoring.system.service.ClassesService;
import com.scoring.system.service.ColleageService;
import com.scoring.system.service.CourseService;
import com.scoring.system.service.CreditService;
import com.scoring.system.service.ExaminationTypeService;
import com.scoring.system.service.JurisdictionService;
import com.scoring.system.service.MajorService;
import com.scoring.system.service.StudentService;
import com.scoring.system.service.TermSerivce;
import com.scoring.system.service.UserService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	private static Logger logger = Logger.getLogger(AdminController.class);
	private static final String password = "123456";
	private Map<String, Object> map;

	@Autowired
	private UserService userService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private JurisdictionService jurisdictionService;

	@Autowired
	private ColleageService colleageService;

	@Autowired
	private ClassesService classesService;

	@Autowired
	private TermSerivce termSerivce;

	@Autowired
	private CourseService courseService;

	@Autowired
	private ClassCurriculumService classCurriculumService;

	@Autowired
	private CreditService creditService;

	@Autowired
	private ExaminationTypeService examinationTypeService;

	@Autowired
	private MajorService majorService;

	@RequestMapping("getAdmin")
	@SkipSecurityCheck
	@ResponseBody
	public Map getAdmin(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		User admin = (User) se.getAttribute("user");
		logger.info("get Admin[" + admin + "]");
		map = new HashMap();
		map.put("admin", admin);
		return map;
	}

	@RequestMapping("updateAdminMessage")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateAdminMeassge(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		HttpSession se = request.getSession();
		User admin = (User) se.getAttribute("user");
		String userId = request.getParameter("user_id");
		String mailbox = request.getParameter("mailbox");
		String phone = request.getParameter("phone");
		logger.info("get AdminMessage[" + "userId=" + userId + ",mailbox=" + mailbox + ",phone=" + phone + "]");
		if (!userId.equals(admin.getUserNumber())) {
			map.put("isSame", "no");
		} else {
			admin.setMailbox(mailbox);
			admin.setPhone(phone);
			int count = userService.updateUser(admin);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "failed");
			}
		}
		return map;
	}

	@RequestMapping("updateAdminPassword")
	@SkipSecurityCheck
	@ResponseBody
	public Map updatePssword(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String oldPassword = request.getParameter("old_password");
		String newPassword = request.getParameter("new_password");
		String confrimPassword = request.getParameter("confirm_password");
		logger.info("get AdminPassword:old_password" + oldPassword + ",new_password=" + newPassword
				+ ",confirm_password=" + confrimPassword);
		HttpSession se = request.getSession();
		User admin = (User) se.getAttribute("user");
		if (!oldPassword.equals(admin.getPassword())) {
			map.put("isExsit", "false");
		} else if (!newPassword.equals(confrimPassword)) {
			map.put("equal", "false");
		} else {
			admin.setPassword(newPassword);
			int count = userService.updateUser(admin);
			if (count > 0) {
				map.put("update", "success");
			} else {
				map.put("update", "false");
			}
		}
		return map;
	}

	@RequestMapping("listUser")
	@SkipSecurityCheck
	@ResponseBody
	public Map listUser(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<User> user = new ArrayList();
		List<User> users = userService.listUser();
		for (User us : users) {
			if (!us.getIsDeleted()) {
				user.add(us);
				logger.info("get userIsDeleted:" + us.getIsDeleted());
			}
			map.put("user", user);
		}
		return map;
	}
	@RequestMapping("listInstructor")
	@SkipSecurityCheck
	@ResponseBody
	public Map listInstructor(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		List<User> user = new ArrayList();
		List<User> users = userService.listUser();
		for (User us : users) {
			String position=us.getJurisdictions().get(0).getName();
			if (!us.getIsDeleted()&&"辅导员".equals(position)) {
				user.add(us);
				logger.info("get userIsDeleted:" + us.getIsDeleted());
			}
			map.put("instructor", user);
		}
		return map;
	}
	@RequestMapping("getInstructorByClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map getInstructorByClass(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String clazz=request.getParameter("class_name");
		Classes cla =classesService.getClassByName(clazz);
		int instructorId =cla.getUserId();
		User instructor=userService.getUserById(instructorId);
		logger.info("get instructor:" +instructor );
		map.put("instructor", instructor);
		return map;
	}
    
	@RequestMapping("getUser")
	@SkipSecurityCheck
	@ResponseBody
	public Map getUser(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		String userId = request.getParameter("user_id");
		logger.info("get userId=" + userId);
		User employee = userService.getUserByUserNumber(userId);
		logger.info("get employee:" + employee.getName());
		se.setAttribute("employee", employee);
		map.put("Employee", employee);
		return map;

	}

	@RequestMapping("deleteUser")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteUser(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String userId = (String) request.getParameter("user_id");
		logger.info("get userId=" + userId);
		User employee = userService.getUserByUserNumber(userId);
		int count = userService.softDeleteUser(employee.getId());
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;

	}

	@RequestMapping("updateUser")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateUser(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String userId = (String) request.getParameter("user_id");
		String name = (String) request.getParameter("name");
		String position = (String) request.getParameter("position");
		logger.info("get user:userId=" + userId + ",name=" + name + ",position=" + position);
		HttpSession se = request.getSession();
		User us = (User) se.getAttribute("employee");
		System.out.println("employee:" + us);
		User employee = userService.getUserByUserNumber(userId);
		UserJurisdiction usJurisdiction = userService.getUserJurisdictionByUserId(employee.getId());
		Jurisdiction jurisdiction = jurisdictionService.getJurisdictionByName(position);
		if (!us.getUserNumber().equals(userId)) {
			logger.info("get userNumber ="+us.getUserNumber()+",userId="+userId);
			map.put("isSame", "no");
		} else {
			employee.setName(name);
			int count1 = userService.updateUser(employee);
			usJurisdiction.setJurisdictionId(jurisdiction.getId());
			int count2 = userService.updateUserJurisdiction(usJurisdiction);
			logger.info("get count1:" + count1);
			logger.info("get count2:" + count2);
			if (count1 > 0 || count2 > 0) {
				map.put("success", "true");
			} else {
				map.put("success", "false");
			}
		}
		return map;
	}

	@RequestMapping("createUser")
	@SkipSecurityCheck
	@ResponseBody
	public Map createUser(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String userId = (String) request.getParameter("user_id");
		String name = (String) request.getParameter("name");
		String position = (String) request.getParameter("position");
		logger.info("get user:userId=" + userId + ",name=" + name + ",position=" + position);
		if (userService.getUserByUserNumber(userId) != null) {
			map.put("isExist", "yes");
		} else {
			User employee = new User();
			UserJurisdiction userJurisdiction = new UserJurisdiction();
			Jurisdiction jurisdiction = jurisdictionService.getJurisdictionByName(position);
			employee.setUserNumber(userId);
			employee.setName(name);
			employee.setPassword(password);
			employee.setIsDeleted(false);
			int count1 = userService.saveUser(employee);
			if (count1 > 0) {
				User em=userService.getUserByUserNumber(userId);
				userJurisdiction.setUserId(em.getId());
				userJurisdiction.setJurisdictionId(jurisdiction.getId());
				int count2 = userService.saveUserJurisdiction(userJurisdiction);
				if (count2 > 0) {
					map.put("success", "true");
				}
			} else {
				map.put("success", "false");
			}
		}
		return map;
	}
    
	@RequestMapping("listColleage")
	@SkipSecurityCheck
	@ResponseBody
	public Map listColleage(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		List<Colleage> colleages = new ArrayList<>();
		List<Colleage> colleage =colleageService.listColleage();
		for(Colleage co : colleage){
			logger.info("get colleage:"+co);
			colleages.add(co);
		}
		map.put("colleage", colleages);
		return map;
		
	}
	@RequestMapping("listMajorByColleage")
	@SkipSecurityCheck
	@ResponseBody
	public Map listMajorByColleage(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		String colleage = request.getParameter("colleage");
		Colleage co = colleageService.getColleageByName(colleage);
		List<Major> majors =new ArrayList<>();
		List<Major> major =majorService.listMajorByColleageId(co.getId());
		for(Major ma : major ){
			logger.info("get major:"+ma);
			majors.add(ma);
		}
		map.put("major", majors);
		return map;
	}
	
	@RequestMapping("listClassByMajor")
	@SkipSecurityCheck
	@ResponseBody
	public Map listClassByMajor(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String major = request.getParameter("major");
		Major ma = majorService.gerMajorByName(major);
		se.setAttribute("majorId", ma.getId());
		logger.info("get major="+ma);
		List<Classes> classes =new ArrayList<>();
		List<Classes> clazz =classesService.listClassByMajorId(ma.getId());
		for(Classes cla : clazz ){
			logger.info("get class:"+cla);
			classes.add(cla);
		}
		map.put("classes", classes);
		return map;
	}
	
	
	@RequestMapping("createClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map createClass(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String code = request.getParameter("class_id");
		String className = request.getParameter("class_name");
		String instructor = request.getParameter("instructor");
		User user = userService.getUserByName(instructor);
		int majorId = (int)(se.getAttribute("majorId"));
		logger.info("get code ="+ code +" ,className=" +className+", InstructorId="+user.getId()+",majorId="+majorId);
        if(classesService.getClassByCode(code)!=null){
        	map.put("isExist", "yes");
        }else{
        	Classes clazz =new Classes();
        	clazz.setCode(code);
        	clazz.setName(className);
        	clazz.setUserId(user.getId());
        	clazz.setMajorId(majorId);
        	clazz.setIsDeleted(false);
        	int count = classesService.saveClass(clazz);
        	if(count>0){
        		map.put("success", "true");
        	}else{
        		map.put("success", "false");
        	}
        }
		return map;
	}
	@RequestMapping("getClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map getClass(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		String classId = request.getParameter("class_id");
		logger.info("get classId=" + classId);
		Classes clazz= classesService.getClassByCode(classId);
		logger.info("get class:" +clazz.getName());
		se.setAttribute("class", clazz);
		map.put("class", clazz);
		return map;

	}
	@RequestMapping("deleteClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteClass(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String classId = (String) request.getParameter("class_id");
		logger.info("get classId=" + classId);
		Classes clazz = classesService.getClassByCode(classId);
		int count = classesService.softDelete(clazz.getId());
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;

	}
	@RequestMapping("updateClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateClass(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String code = request.getParameter("class_id");
		String className = request.getParameter("class_name");
		String instructor = request.getParameter("instructor");
		User user = userService.getUserByName(instructor);
		Classes clazz =(Classes)se.getAttribute("class");
		logger.info("get code ="+ code +" ,className=" +className+", InstructorId="+user.getId());
        if(user.getName()==null){
			map.put("isExist", "no");
		}
		else{
			clazz.setCode(code);
			clazz.setName(className);
			clazz.setUserId(user.getId());
			int count = classesService.updateClass(clazz);
			if(count>0){
				map.put("update", "true");
			}else{
				map.put("update", "false");
			}
		}
		return map;
	}
	
	@RequestMapping("listCourseByColleage")
	@SkipSecurityCheck
	@ResponseBody
	public Map listCourseByColleage(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String colleage = request.getParameter("colleage");
		Colleage coll = colleageService.getColleageByName(colleage);
		se.setAttribute("colleageId", coll.getId());
		List<Course> course =new ArrayList<>();
		List<Course> courses =courseService.listCourseByColleageId(coll.getId());
		for(Course co : courses ){
			if(co.getIsDeleted()!=true){
				logger.info("get course:"+co);
				course.add(co);
			}
		}
		map.put("course", course);
		return map;
	}
	@RequestMapping("createCourse")
	@SkipSecurityCheck
	@ResponseBody
	public Map createCourse(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String code = request.getParameter("course_id");
		String courseName = request.getParameter("course_name");
		int colleageId = (int)(se.getAttribute("colleageId"));
		logger.info("get code ="+ code +" ,courseName=" +courseName+",colleageId="+colleageId);
        if(courseService.getCourseBycode(code)!=null){
        	map.put("isExist", "yes");
        }else{
        	Course course =new Course();
        	course.setCode(code);
        	course.setName(courseName);
        	course.setColleageId(colleageId);
        	course.setIsDeleted(false);
        	int count = courseService.saveCourse(course);
        	if(count>0){
        		map.put("success", "true");
        	}else{
        		map.put("success", "false");
        	}
        }
		return map;
	}
	
	@RequestMapping("getCourse")
	@SkipSecurityCheck
	@ResponseBody
	public Map getCourse(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		String courseId = request.getParameter("course_id");
		logger.info("get courseId=" + courseId);
	    Course course =courseService.getCourseBycode(courseId);
		logger.info("get course:" +course.getName());
		se.setAttribute("course", course);
		map.put("course", course);
		return map;

	}
	@RequestMapping("deleteCourse")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteCourse(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String courseId = (String) request.getParameter("course_id");
		logger.info("get courseId=" + courseId);
		Course course =courseService.getCourseBycode(courseId);
		int count = courseService.softDelete(course.getId());
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;

	}
	@RequestMapping("updateCourse")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateCourse(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String code = request.getParameter("course_id");
		String courseName = request.getParameter("course_name");
		Course course =(Course)se.getAttribute("course");
		logger.info("get code ="+ code +" ,courseName=" +courseName);
		course.setCode(code);
		course.setName(courseName);
		int count = courseService.updateCourse(course);
		if(count>0){
			map.put("update", "true");
		}else{
			map.put("update", "false");
		}
		return map;
	}
	
	@RequestMapping("listTermByClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listTermByClass(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		String clazz = request.getParameter("class_name");
		logger.info("get class="+clazz);
		int index= clazz.indexOf("2");
		int year =Integer.valueOf(clazz.substring(index,index+4));
		logger.info("get year="+year);
		List<Term> term =new ArrayList<>();
		List<Term> terms =termSerivce.listTerm();
		for(Term te : terms ){
			logger.info("get term:"+te);
			int year1=Integer.valueOf(te.getName().substring(0, 4));
			logger.info("get year1="+year1);
			if(year-year1==0||year-year1>=-3){
				term.add(te);
			}
		}
		map.put("term", term);
		return map;
	}
	@RequestMapping("listClassCourseByClassAndTerm")
	@SkipSecurityCheck
	@ResponseBody
	public Map listClassCourseByClassAndTerm(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se =request.getSession();
		String clazz= request.getParameter("class_name");
		String term= request.getParameter("term");
		Classes cla=classesService.getClassByName(clazz);
		se.setAttribute("classId", cla.getId());
		List<ClassCurriculum> ccm =new ArrayList<>();
		List<ClassCurriculum> ccms =classCurriculumService.listClassCurriculumByClassIdAndTerm(cla.getId(), term);
		for(ClassCurriculum classCurriculum : ccms ){
			logger.info("get classCurriculum:"+classCurriculum);
			if(classCurriculum.getIsDeleted()!=true){
				ccm.add(classCurriculum);
			}
		}
		map.put("classCurriculum", ccm);
		return map;
	}
	@RequestMapping("listCredit")
	@SkipSecurityCheck
	@ResponseBody
	public Map listCredit(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		List<Credit> credit =new ArrayList<>();
		List<Credit> credits =creditService.listCredit();
		for(Credit cre : credits ){
			logger.info("get credit:"+cre);
			credit.add(cre);
		}
		map.put("credit", credit);
		return map;
	}
	@RequestMapping("listType")
	@SkipSecurityCheck
	@ResponseBody
	public Map listType(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		List<ExaminationType> type =new ArrayList<>();
		List<ExaminationType> types =examinationTypeService.listExaminationType();
		for(ExaminationType tp : types ){
			logger.info("get ExaminationType:"+tp);
			type.add(tp);
		}
		map.put("type", type);
		return map;
	}
	@RequestMapping("createClassCurriculum")
	@SkipSecurityCheck
	@ResponseBody
	public Map createClassCurriculum(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String term = request.getParameter("term");
		String courseName = request.getParameter("course_name");
		String type=request.getParameter("type");
		String credit=request.getParameter("credit");
		String teacher=request.getParameter("teacher");
		int classId = (int)(se.getAttribute("classId"));
		logger.info("get term ="+ term +" ,courseName=" +courseName+",classId="+classId+",type="+type+",credit="+credit+",teacher="+teacher);
        Course course=courseService.getCourseByName(courseName);
        ClassCurriculum classCurriculum=new ClassCurriculum();
        classCurriculum.setTerm(term);
        classCurriculum.setClassId(classId);
        classCurriculum.setCourseId(course.getId());
        classCurriculum.setCurriculumNature(type);
        classCurriculum.setCredit(credit);
        classCurriculum.setTeacher(teacher);
        classCurriculum.setIsDeleted(false);
        int count = classCurriculumService.saveClassCurriculum(classCurriculum);
        if(count>0){
        	map.put("success", "true");
        }else{
        	map.put("success", "false");
        }
		return map;
	}
	
	@RequestMapping("getClassCurriculum")
	@SkipSecurityCheck
	@ResponseBody
	public Map getClassCurriculum(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get id=" + id);
		ClassCurriculum classCurriculum=classCurriculumService.getClassCurriculumById(id);
		logger.info("get classCurriculum:" +classCurriculum.getCourseId());
		se.setAttribute("classCurriculum", classCurriculum);
		map.put("classCurriculum", classCurriculum);
		return map;

	}
	@RequestMapping("deleteClassCurriculum")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteClassCurriculum(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		int id = Integer.valueOf(request.getParameter("id"));
		logger.info("get id=" + id);
		int count = classCurriculumService.softDelete(id);
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;

	}
	@RequestMapping("updateClassCurriculum")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateClassCurriculum(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String courseName = request.getParameter("course_name");
		String type = request.getParameter("type");
		String credit = request.getParameter("credit");
		String teacher = request.getParameter("teacher");
		ClassCurriculum classCurriculum=(ClassCurriculum)se.getAttribute("classCurriculum");
		logger.info("get courseName ="+ courseName +" ,type=" +type+",credit="+credit+",teacher="+teacher);
        Course course =courseService.getCourseByName(courseName);
        classCurriculum.setCourseId(course.getId());
        classCurriculum.setCurriculumNature(type);
        classCurriculum.setCredit(credit);
        classCurriculum.setTeacher(teacher);
		int count = classCurriculumService.updateClassCurriculum(classCurriculum);
	    if(count>0){
			map.put("update", "true");
		}else{
			map.put("update", "false");
		}
		return map;
	}
	@RequestMapping("listStudentByClass")
	@SkipSecurityCheck
	@ResponseBody
	public Map listStudentByClass(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se =request.getSession();
		String clazz= request.getParameter("class_name");
		Classes cla=classesService.getClassByName(clazz);
		se.setAttribute("classId", cla.getId());
		List<Student> student =new ArrayList<>();
		List<Student> students =studentService.listStudentByClassId(cla.getId());
		for(Student stu : students ){
			logger.info("get student:"+stu);
			if(stu.getIsDeleted()!=true){
				student.add(stu);
			}
		}
		map.put("student", student);
		return map;
	}
	@RequestMapping("createStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map createStudent(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String studentId = request.getParameter("student_id");
		String studentName = request.getParameter("student_name");
		String phone=request.getParameter("phone");
		String instructor=request.getParameter("instructor");
		int classId = (int)(se.getAttribute("classId"));
		logger.info("get studentId ="+ studentId +" ,studentName=" +studentName+",phone="+phone+",instructor="+instructor);
		User user = userService.getUserByName(instructor);
		
		if(studentService.getStudentByStudentNumber(studentId)!=null){
			map.put("isExist", "yes");
		}
		else if(user==null){
			map.put("isExist", "no");
		}else{
			Student student = new Student();
			student.setStudentNumber(studentId);
			student.setName(studentName);
			student.setPassword(password);
			student.setPhone(phone);
	        student.setClassId(classId);
	        student.setUserId(user.getId());
			student.setIsDeleted(false);
			int count = studentService.saveStudent(student);
			if(count>0){
				map.put("success", "true");
			}else{
				map.put("success", "false");
			}
		}
		return map;
	}
	
	@RequestMapping("getStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map getStudent(HttpServletRequest request, ModelMap modelMap) {
		HttpSession se = request.getSession();
		map = new HashMap();
		String studentId = request.getParameter("student_id");
		logger.info("get studentId=" + studentId);
		Student student=studentService.getStudentByStudentNumber(studentId);
		logger.info("get student:" +student.getName());
		se.setAttribute("student", student);
		map.put("student", student);
		return map;

	}
	@RequestMapping("deleteStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map deleteStudent(HttpServletRequest request, ModelMap modelMap) {
		map = new HashMap();
		String studentId = request.getParameter("student_id");
		logger.info("get studentId=" + studentId);
		Student student=studentService.getStudentByStudentNumber(studentId);
		logger.info("get student:" +student.getName());
		int count = studentService.softDeleteStudent(student.getId());
		if (count > 0) {
			map.put("isDelete", "success");
		} else {
			map.put("isDelete", "failed");
		}
		return map;

	}
	@RequestMapping("updateStudent")
	@SkipSecurityCheck
	@ResponseBody
	public Map updateStudent(HttpServletRequest request, ModelMap modelMap){
		map = new HashMap();
		HttpSession se = request.getSession();
		String studentId =request.getParameter("student_id");
		String stduentName = request.getParameter("student_name");
		String phone = request.getParameter("phone");
		String instructor = request.getParameter("instructor");
		Student student =(Student)se.getAttribute("student");
		logger.info("get studentId"+studentId+",stduentName ="+ stduentName +" ,phone=" +phone+",instructor="+instructor);
		User user = userService.getUserByName(instructor);
		student.setStudentNumber(studentId);
		student.setName(stduentName);
        student.setPhone(phone);
        student.setUserId(user.getId());
		int count = studentService.updateStudent(student);
			if(count>0){
				map.put("update", "true");
			}else{
				map.put("update", "false");
			}

		return map;
	}
	
}
